; Exercise 1.7b
;
; An alternative strategy for implementing good-enough? is to watch how guess
; changes from one iteration to the next and to stop when the change is a very
; small fraction of the guess. Design a square-root procedure that uses this
; kind of end test. Does this work better for small and large numbers?

(define (sqrt x)
  (define (good-enough? old-guess guess)
    (= old-guess guess))

  (define (improve guess)
    (average guess (/ x guess)))

  (define (average x y)
    (/ (+ x y) 2))

  (define (sqrt-iter old-guess guess)
    (if (good-enough? old-guess guess)
      guess
      (sqrt-iter guess (improve guess))))

  (sqrt-iter 2.0 1.0))

; The new-sqrt is able to compute the sqrt from 1e+308 to 1e-323, and it seems
; to be limited by chicken scheme's numeric precision. :)

; vim: set lisp expandtabs smarttabs

